﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid;
using ESOL.Common.Entity;
using ESOL.Common;
using ESOL.Business.Mst;
using ESOL.Business.Prod;
using ESOL.Business.Proj;
using ESOL.WinUI.Bpt;

namespace ESOL.WinUI.Proj
{
    public partial class frmMaintainMBOMTechnic : BaseForm.frmBaseDialogyMaintainForm
    {
        #region vars
        private MstProductEntity mSelectedProduct;
        #endregion

        #region ctors
        public frmMaintainMBOMTechnic(MstProductEntity entity)
        {
            InitializeComponent();
            mSelectedProduct = entity;
        }
        public frmMaintainMBOMTechnic()
        {
            InitializeComponent();
        }
        #endregion

        #region functions
        private void BindComobox()
        {
            DataTable dt = MstCommonItemBs.GetMstCommonItemDtByType("STT");
            repositoryItemGridLookUpEdit1.DataSource = dt;
            repositoryItemGridLookUpEdit1.DisplayMember = "ITEM_NAME";
            repositoryItemGridLookUpEdit1.ValueMember = "ITEM_CODE";

            repositoryItemGridLookUpEdit2.DataSource = dt;
            repositoryItemGridLookUpEdit2.DisplayMember = "ITEM_NAME";
            repositoryItemGridLookUpEdit2.ValueMember = "ITEM_CODE";
        }

        private void SetField()
        {
            if (mSelectedProduct == null)
                return;
            txtProductName.Text = mSelectedProduct.ProductName;
            txtProductCode.Text = mSelectedProduct.ProductCode;
        }

        private void LoadSourceStation()
        {
            string sCondition = " AND A.DELETE_FLAG='" + 0 + "' ";
            DataTable dt = ProdTechnicBs.GetProdTechnicByCondition("", sCondition, "");
            gvStations.DataSource = dt;
        }

        private void LoadTargetTechnic()
        {
            string sCondition = " AND A.TECHNIC_ID=''";
            DataTable dt = ProdTechnicBs.GetProdTechnicByCondition("", sCondition, "");
            dt.Columns.Add("STD_WORKING_TIME", typeof(decimal));
            dt.Columns.Add("REMARK", typeof(string));
            if (mSelectedProduct != null)
            {
                DataTable dtBomTechnic = ProjMBOMTechnicBs.GetDtAllByProductID(mSelectedProduct.ProductId);
                if (dtBomTechnic.Rows.Count > 0)
                {
                    foreach (DataRow drBomTechnic in dtBomTechnic.Rows)
                    {
                        DataRow dr = dt.NewRow();
                        dr["TECHNIC_ID"] = drBomTechnic["TECHNIC_ID"];
                        dr["TECHNIC_CODE"] = drBomTechnic["TECHNIC_CODE"];
                        dr["TECHNIC_NAME"] = drBomTechnic["TECHNIC_NAME"];
                        dr["STATION_TYPE"] = drBomTechnic["STATION_TYPE"];
                        dr["GROUP_ID"] = drBomTechnic["GROUP_ID"];
                        dr["STD_WORKING_TIME"] = drBomTechnic["STD_WORKING_TIME"];
                        dr["REMARK"] = drBomTechnic["REMARK"];
                        dt.Rows.Add(dr);
                    }
                }
            }
            gvTargetStations.DataSource = dt;
        }

        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindComobox();
            SetField();
            LoadSourceStation();
            LoadTargetTechnic();
        }

        private GridHitInfo downHitInfo;
        private void gridView2_MouseDown(object sender, MouseEventArgs e)
        {
            downHitInfo = gridView2.CalcHitInfo(new Point(e.X, e.Y));
        }

        private void gridView2_MouseMove(object sender, MouseEventArgs e)
        {
            if (downHitInfo == null) return;
            if (e.Button != MouseButtons.Left) return;
            Rectangle dragRect = new Rectangle(new Point(
                downHitInfo.HitPoint.X - SystemInformation.DragSize.Width / 2,
                downHitInfo.HitPoint.Y - SystemInformation.DragSize.Height / 2), SystemInformation.DragSize);
            if (!dragRect.Contains(new Point(e.X, e.Y)))
            {
                DataRow data = gridView2.GetDataRow(downHitInfo.RowHandle);
                int[] rows = gridView2.GetSelectedRows();
                List<DataRow> row = new List<DataRow>();
                for (int i = 0; i < rows.Length; i++)
                    row.Add(gridView2.GetDataRow((int)rows[i]));
                gvStations.DoDragDrop(row, DragDropEffects.Copy);
            }
        }

        private void gvTargetStations_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void gvTargetStations_DragDrop(object sender, DragEventArgs e)
        {
            GridControl grid = sender as GridControl;
            DataTable table = grid.DataSource as DataTable;
            List<DataRow> row = e.Data.GetData(typeof(List<DataRow>)) as List<DataRow>;
            if (row != null && table != null)
            {
                if (row.Count > 0)
                {
                    for (int i = 0; i < row.Count; i++)
                    {
                        table.ImportRow(row[i]);
                        //row[i].Delete();  // 把原有的数据行删除。
                    }
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int[] arrySelectedIndex = gridView2.GetSelectedRows();
            if (arrySelectedIndex.Length == 0)
            {
                MessageBox.Show("请选择工序！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DataTable dtTarget = (DataTable)gvTargetStations.DataSource;
            for (int i = 0; i < arrySelectedIndex.Length; i++)
            {
                dtTarget.ImportRow(gridView2.GetDataRow(arrySelectedIndex[i]));
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            int[] arrySelectedIndex = gridView1.GetSelectedRows();
            if (arrySelectedIndex.Length == 0)
            {
                MessageBox.Show("请选择工序！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            List<DataRow> list = new List<DataRow>();
            for (int i = 0; i < arrySelectedIndex.Length; i++)
            {
                DataRow dr = gridView1.GetDataRow(arrySelectedIndex[i]);
                list.Add(dr);
            }
            DataTable dt = (DataTable)gvTargetStations.DataSource;
            foreach (DataRow dr in list)
            {
                dt.Rows.Remove(dr);
            }
        }

        private void btnUpMove_Click(object sender, EventArgs e)
        {
            int[] arrySelectedIndex = gridView1.GetSelectedRows();
            if (arrySelectedIndex.Length == 0)
            {
                MessageBox.Show("请选择工序！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else if (arrySelectedIndex.Length > 1)
            {
                MessageBox.Show("请选择一条需要调整的工序！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (arrySelectedIndex[0] == 0)
                return;
            DataTable dt = (DataTable)gvTargetStations.DataSource;
            DataRow drTemp = dt.NewRow();
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                drTemp[i] = dt.Rows[arrySelectedIndex[0]][i];
            }
            dt.Rows.Remove(gridView1.GetDataRow(arrySelectedIndex[0]));
            dt.Rows.InsertAt(drTemp, arrySelectedIndex[0] - 1);
            gridView1.FocusedRowHandle = arrySelectedIndex[0] - 1;
        }

        private void btnDownMove_Click(object sender, EventArgs e)
        {
            int[] arrySelectedIndex = gridView1.GetSelectedRows();
            if (arrySelectedIndex.Length == 0)
            {
                MessageBox.Show("请选择工序！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else if (arrySelectedIndex.Length > 1)
            {
                MessageBox.Show("请选择一条需要调整的工序！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DataTable dt = (DataTable)gvTargetStations.DataSource;
            if (arrySelectedIndex[0] == dt.Rows.Count - 1)
                return;
            DataRow drTemp = dt.NewRow();
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                drTemp[i] = dt.Rows[arrySelectedIndex[0]][i];
            }
            dt.Rows.Remove(gridView1.GetDataRow(arrySelectedIndex[0]));
            dt.Rows.InsertAt(drTemp, arrySelectedIndex[0] + 1);
            gridView1.FocusedRowHandle = arrySelectedIndex[0] + 1;
        }

        private void txtProductCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainProductQuery frm = new frmMaintainProductQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedProduct != null)
            {
                mSelectedProduct = frm.SelectedProduct;
                txtProductCode.Text = mSelectedProduct.ProductCode;
                txtProductName.Text = mSelectedProduct.ProductName;
                LoadTargetTechnic();
            }
        }

        protected override void Click_Save()
        {
            DataTable dt = (DataTable)gvTargetStations.DataSource;
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("流程不能为空，保存失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (ProjMBOMTechnicBs.SaveStdTechnic(mSelectedProduct, dt))
            {
                MessageBox.Show("保存成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("保存失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        #endregion
    }
}
